package com.yohann.boot.controller;

import com.yohann.boot.common.annotation.Cache30Minutes;
import com.yohann.boot.common.annotation.EnumValid;
import com.yohann.boot.common.annotation.Lock;
import com.yohann.boot.common.enums.TestEnum2;
import com.yohann.boot.common.response.Result;
import com.yohann.boot.entity.TestElasticsearch;
import com.yohann.boot.entity.TestEntity;
import com.yohann.boot.mapper.TestMapper;
import com.yohann.boot.pojo.TestVO;
import com.yohann.boot.service.TestService;
import com.yohann.boot.util.ConvertUtils;
import com.yohann.boot.util.TokenUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 测试控制器
 *
 * @author Yeung Wah
 * @since 2021/4/17
 */
@Validated
@Api(tags = "测试控制器")
@RestController
@AllArgsConstructor
public class TestController {
    private final TestService testService;
    private final TestMapper testMapper;

    @GetMapping("/test1")
    @Cache30Minutes
    @Lock(key = "#entity.name")
    public Result<List<String>> test1(@Validated TestEntity entity) {
        return Result.success(Arrays.asList("a", "b", "c", "d"));
    }

    @GetMapping("/test2")
    public Result<String> test2(@RequestParam(required = false) @EnumValid(clazz = TestEnum2.class) Integer code) {
        System.out.println(code);
        Map<String, String> map = new HashMap<>();
        String encode = TokenUtils.encode(map);
        return Result.success(encode);
    }

    @GetMapping("/test3")
    public Result<Map<String, String>> test3(String token) {
        TestEntity testEntity = new TestEntity();
        testEntity.setId(1);
        testEntity.setName("abc");
        testEntity.setAge(18);
        TestEntity testEntity2 = new TestEntity();
        testEntity2.setName("dfg");
        testEntity2.setAge(19);
        testMapper.duplicateKeyUpdate(Arrays.asList(testEntity, testEntity2));
        return Result.success((Map<String, String>) null);
    }

    @GetMapping("/test4")
    @Lock
    public Result<List<TestEntity>> test4() {
        TestEntity testEntity = new TestEntity();
        testEntity.setAge(1);
        ConvertUtils.convertEnums(testEntity);
        return Result.success(Collections.singletonList(testEntity));
    }

    @ApiOperation("测试接口")
    @PostMapping("/test5")
    public Result<Object> test5(@ApiParam(value = "参数", type = "TestVO") @RequestBody TestVO vo) {
        return Result.success(testService.test(vo));
    }

    @GetMapping("/testThreadPoll")
    public Result<String> testThreadPoll() {
        testService.testThreadPoll();
        return Result.success();
    }

    @GetMapping("/send/{message}")
    public Result<String> send(@PathVariable String message) {
        testService.send(message);
        return Result.success();
    }

    @GetMapping("/send/{message}/{ttl}")
    public Result<String> send(@PathVariable String message, @PathVariable String ttl) {
        testService.send(message, ttl);
        return Result.success();
    }

    @GetMapping("/date")
    public Result<Date> send() {
        return Result.success(new Date());
    }

    @GetMapping("/sendEmail/{email}")
    public Result<String> sendEmail(@PathVariable("email") String email) {
        testService.sendEmail(email);
        return Result.success();
    }

    @GetMapping("/es")
    public Result<Iterable<TestElasticsearch>> getEs(String content) {
        Iterable<TestElasticsearch> list = testService.getEs(content);
        return Result.success(list);
    }

    @PostMapping("/es")
    public Result<String> saveEs(@RequestBody TestElasticsearch elasticsearch) {
        testService.saveEs(elasticsearch);
        return Result.success();
    }
}